package com.campsys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.common.Result;
import com.campsys.entity.TeachingUnit;
import com.campsys.entity.LearningRecord;
import com.campsys.entity.User;
import com.campsys.service.TeachingUnitService;
import com.campsys.service.dto.TeachingUnitDTO;
import com.campsys.service.dto.TeachingUnitQueryDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/teacher/unit")
@Slf4j
public class TeacherUnitController {
    
    @Autowired
    private TeachingUnitService unitService;
    
    @RequiresRoles("teacher")
    @RequiresPermissions("unit:view")
    @PostMapping("/list")
    public Result getUnitList(@RequestBody TeachingUnitQueryDTO queryDTO) {
        User teacher = (User) SecurityUtils.getSubject().getPrincipal();
        Page<TeachingUnit> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        return Result.ok(unitService.getUnitList(page, queryDTO, teacher.getId()));
    }
    
    @RequiresRoles("teacher")
    @RequiresPermissions("unit:add")
    @PostMapping("/add")
    public Result addUnit(@RequestBody @Validated TeachingUnitDTO unitDTO) {
        User teacher = (User) SecurityUtils.getSubject().getPrincipal();
        unitService.addUnit(unitDTO, teacher.getId());
        return Result.ok();
    }
    
    @RequiresRoles("teacher")
    @RequiresPermissions("unit:edit")
    @PostMapping("/update")
    public Result updateUnit(@RequestBody @Validated TeachingUnitDTO unitDTO) {
        if (unitDTO.getId() == null) {
            return Result.error("教学单元ID不能为空");
        }
        User teacher = (User) SecurityUtils.getSubject().getPrincipal();
        unitService.updateUnit(unitDTO, teacher.getId());
        return Result.ok();
    }
    
    @RequiresRoles("teacher")
    @RequiresPermissions("unit:delete")
    @PostMapping("/delete/{id}")
    public Result deleteUnit(@PathVariable Long id) {
        User teacher = (User) SecurityUtils.getSubject().getPrincipal();
        unitService.deleteUnit(id, teacher.getId());
        return Result.ok();
    }
    
    @RequiresRoles("teacher")
    @RequiresPermissions("unit:edit")
    @PostMapping("/status/{id}")
    public Result updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        User teacher = (User) SecurityUtils.getSubject().getPrincipal();
        unitService.updateStatus(id, status, teacher.getId());
        return Result.ok();
    }
    
    @RequiresRoles("teacher")
    @RequiresPermissions("unit:view")
    @PostMapping("/{id}/records")
    public Result getLearningRecords(@PathVariable Long id,
                                   @RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize) {
        User teacher = (User) SecurityUtils.getSubject().getPrincipal();
        Page<LearningRecord> page = new Page<>(pageNum, pageSize);
        return Result.ok(unitService.getUnitLearningRecords(page, id, teacher.getId()));
    }
} 